import logging
import time

from bases.atest_case import ATestCase


class ATestCaseAll:
    def __init__(self,
                 main_cases:list[ATestCase]=None,
                 init_cases:list[ATestCase]=None,
                 clear_cases:list[ATestCase]=None,
                 scan_sn: list[ATestCase] = None,
                 pre_main_cases:list[ATestCase]=None,
                 post_main_cases: list[ATestCase] = None,
                 ):
        # 预计还要添加两个用例部分
        if main_cases is None:
            main_cases = []
        if init_cases is None:
            init_cases = []
        if clear_cases is None:
            clear_cases = []
        if scan_sn is None:
            scan_sn = []
        if pre_main_cases is None:
            pre_main_cases = []
        if post_main_cases is None:
            post_main_cases = []

        self.init_case_list:list[ATestCase] = init_cases

        self.scan_sn_list:list[ATestCase] = scan_sn
        self.pre_main_case_list:list[ATestCase] = pre_main_cases
        self.post_main_case_list:list[ATestCase] = post_main_cases
        self.main_case_list:list[ATestCase] = main_cases
        self.clear_case_list:list[ATestCase] = clear_cases

        # self._dot = ['init', 'scan_sn', 'pre_main', 'main', 'post_main', 'clear']

        # 用于添加用例
        self._case_dict:dict[str:list[ATestCase]] = {
            'init':self.init_case_list,
            'scan_sn': self.scan_sn_list,
            'pre_main': self.pre_main_case_list,
            'main': self.main_case_list,
            'post_main': self.post_main_case_list,
            'clear': self.clear_case_list,
        }


        self.isPause = False
        # self.case_order = 0  # 运行顺序

        self.ptrCaseIndex = 0       # 运行用例的位置 下标----已废弃

    # 与窗口交互，需要更细节的窗口控制，暂时决定弃用
    def run(self):
        # for testcase in self.test_case_list:
        #     testcase.run()      # 顺序执行每个用例
        #     self.case_order += 1
        #     print(self.count_2_str(), testcase)     # 这是一个输出
        ### 初始化用例
        while self.init_case_list:
            self.init_case_list[self.ptrCaseIndex].run()
            print('init',self.count_2_str(), self.init_case_list[self.ptrCaseIndex])

            self.ptrCaseIndex += 1
            if self.ptrCaseIndex == len(self.init_case_list):
                break
        print('\033[31m'+'init_case 执行完毕'+'\033[0m')
        self.ptrCaseIndex = 0



        ### 用例执行
        while self.main_case_list:
            self.main_case_list[self.ptrCaseIndex].run()
            print('test',self.count_2_str(), self.main_case_list[self.ptrCaseIndex])

            self.ptrCaseIndex += 1
            if self.ptrCaseIndex == len(self.main_case_list):
                logging.debug('test_case 执行完毕')
                break
        self.ptrCaseIndex = 0

        ### 复位用例——硬件资源
        while self.clear_case_list:
            self.clear_case_list[self.ptrCaseIndex].run()
            print(self.count_2_str(), self.clear_case_list[self.ptrCaseIndex])

            self.ptrCaseIndex += 1
            if self.ptrCaseIndex == len(self.clear_case_list):
                logging.debug('clear_case 执行完毕')
                break
        self.ptrCaseIndex = 0

    # 为每个子节点，数组添加用例
    def add_case(self,case:ATestCase, index=None, flag='main'):
        if index is None:
            self._case_dict[flag].append(case)
        else:
            self._case_dict[flag].insert(index, case)

    def add_cases(self, cases:list[ATestCase], index=None, flag='main'):
        for case in cases:
            self.add_case(case, index, flag)

    def count_2_str(self):
        return '('+str(self.ptrCaseIndex+1).rjust(3,'0')+')'

    # 有关运行位置控制的方法
    def index_add(self):
        self.ptrCaseIndex += 1

    def index_clear(self):
        self.ptrCaseIndex = 0

    def index_set(self, index=0):
        self.ptrCaseIndex = index

    # 已废弃
    def get_all_cases(self):
        return self.init_case_list, self.main_case_list, self.clear_case_list

    def get_all_cases_2(self):
        return (self.init_case_list,        # 0
                self.scan_sn_list,          # 1
                self.pre_main_case_list,    # 2
                self.main_case_list,        # 3
                self.post_main_case_list,   # 4
                self.clear_case_list)       # 5

    def get_main_cases(self):
        return self.main_case_list

    def get_order_case(self):
        pass

    # 设置相应的用例在窗口显示的标签
    def window_case_tag_testing(self):
        pass
    def window_case_tag_pass(self):
        pass
    def window_case_tag_fail(self):
        pass